//
//  YSApplicationCell.m
//  YaSha-iOS
//
//  Created by 方鹏俊 on 16/11/23.
//  Copyright © 2016年 方鹏俊. All rights reserved.
//

#import "YSApplicationCell.h"

@interface YSApplicationCell ()

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) QMUILabel *messageNumberLabel;

@end

@implementation YSApplicationCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    _iconImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.iconImageView];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(28, 28));
    }];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:12];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.textColor = [UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.00];
    [self.contentView addSubview:self.nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.top.mas_equalTo(_iconImageView.mas_bottom).offset(12);
        make.height.mas_equalTo(12);
        make.width.mas_greaterThanOrEqualTo(0);
    }];
    
    _messageNumberLabel = [[QMUILabel alloc] init];
    _messageNumberLabel.textColor = UIColorWhite;
    _messageNumberLabel.textAlignment = NSTextAlignmentCenter;
    _messageNumberLabel.layer.masksToBounds = YES;
    _messageNumberLabel.layer.cornerRadius = 10*kWidthScale;
    _messageNumberLabel.backgroundColor = UIColorRed;
    [self.contentView addSubview:self.messageNumberLabel];
    [_messageNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_iconImageView.mas_top).offset(-4);
        make.right.mas_equalTo(_iconImageView.mas_right).offset(4);
        make.size.mas_equalTo(CGSizeMake(0, 0));
    }];
}

- (void)setCellModel:(YSApplicationModel *)cellModel {
    _cellModel = cellModel;
    if ([cellModel.imageName isKindOfClass:[UIImage class]]) {
        _iconImageView.image = cellModel.imageName;
    }else{//图片名字
        _iconImageView.image = [UIImage imageNamed:_cellModel.imageName];
    }
   
    _nameLabel.text = _cellModel.name;
    if (_cellModel.unreadCount > 99) {
        _messageNumberLabel.text = @"99+";
        _messageNumberLabel.font = [UIFont systemFontOfSize:8];
    } else {
        _messageNumberLabel.text = [NSString stringWithFormat:@"%zd", _cellModel.unreadCount];
        _messageNumberLabel.font = [UIFont systemFontOfSize:10];
    }
    [_messageNumberLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_iconImageView.mas_top).offset(-10);
        make.right.mas_equalTo(_iconImageView.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(_cellModel.unreadCount == 0 ? 0 : 20*kWidthScale, _cellModel.unreadCount == 0 ? 0 : 20*kWidthScale));
    }];
}

@end
